package me.zhengjie.network;

import me.zhengjie.dao.MaterialDao;
import me.zhengjie.enums.MesRequsetEnum;
import me.zhengjie.vo.freeze.FreezeNestVo;
import me.zhengjie.vo.freeze.FreezeVo;
import me.zhengjie.vo.mes.ResponseResult;
import me.zhengjie.vo.mes.RetNestVo;
import me.zhengjie.vo.mes.StockSyncVo;
import me.zhengjie.vo.mes.UnlockNestVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MesNetworkRequest extends BaseNetworkRequest {

    // 入库同步
    public static ResponseResult<?> InventorySynchronizationRequest(String containerCode,
                                                                 String oldStorageCode, String oldWarehouse,
                                                                 String storageId, String newWarehouse) {
        StockSyncVo request = new StockSyncVo();
        request.setContainerCode(containerCode);
        request.setOldStorageCode(oldStorageCode);
        request.setOldWarehouse(oldWarehouse);
        request.setNewStorageCode(storageId);
        request.setOldWarehouse(newWarehouse);
        String requestURL = MesRequsetEnum.api_url_mes.api + MesRequsetEnum.inbound_stock_sync.api;
        return requestMES("入库同步", request, requestURL);
    }

    // 物料冻结
    public static ResponseResult<?> materialFreezeRequest(String containerCode, List<MaterialDao> materialList) {
        FreezeNestVo request = new FreezeNestVo();
        request.setContainerCode(containerCode);
        for (MaterialDao materialDao : materialList) {
            request.setItemBarCode(materialDao.getItemBarCode());
        }
        String requestURL = MesRequsetEnum.api_url_mes.api + MesRequsetEnum.material_freeze.api;
        return requestMES("物料冻结", request, requestURL);
    }

    // 物料解冻
    public static ResponseResult<?> materialUnfreezeReQuest(String containerCode, List<String> materialList) {
        FreezeNestVo request = new FreezeNestVo();
        request.setContainerCode(containerCode);
        request.setItemBarCodeList(materialList);
        String requestURL = MesRequsetEnum.api_url_mes.api + MesRequsetEnum.material_unfreeze.api;
        return requestMES("物料解冻", request, requestURL);
    }

    // 解嵌套
    public static ResponseResult<?> outboundUnNestingRequest(String containerCode, List<String> ItemBarCodeList, List<MaterialDao> materialList) {
        if (containerCode == null || containerCode.isEmpty()) {
            return ResponseResult.error("请求体不能为空");
        }
        if ((ItemBarCodeList == null || ItemBarCodeList.isEmpty()) &&
                (materialList == null || materialList.isEmpty())) {
            return ResponseResult.error("请求体不能为空");
        }
        if (ItemBarCodeList == null || ItemBarCodeList.isEmpty()) {
            ItemBarCodeList = new ArrayList<>();
            for (MaterialDao materialDao : materialList) {
                ItemBarCodeList.add(materialDao.getItemBarCode());
            }
        }
        String requestURL = MesRequsetEnum.api_url_mes.api + MesRequsetEnum.outbound_unnesting.api;
        UnlockNestVo request = new UnlockNestVo(containerCode, ItemBarCodeList);
        return requestMES("解嵌套", request, requestURL);
    }

    // 回库嵌套
    public static ResponseResult<?> nestedReturnToLibraryRequest(List<MaterialDao> materialVoData, String containerCode) {
        if (materialVoData.isEmpty() || containerCode == null || containerCode.isEmpty()) {
            return ResponseResult.error("请求体不能为空");
        }
        Map<String, Double> itemBarMap = new HashMap<>();
        for (MaterialDao datum : materialVoData) {
            String itemBarCode = datum.getItemBarCode();
            if (itemBarMap.containsKey(itemBarCode)) {
                itemBarMap.put(itemBarCode, itemBarMap.get(itemBarCode) + datum.getInventoryQuantity());
            } else {
                itemBarMap.put(itemBarCode, datum.getInventoryQuantity());
            }
        }
        RetNestVo request = new RetNestVo(containerCode);
        for (Map.Entry<String, Double> entry : itemBarMap.entrySet()) {
            request.setRetNestSingleVo(entry.getKey(), entry.getValue());
        }
        String requestURL = MesRequsetEnum.api_url_mes.api + MesRequsetEnum.inbound_nesting_sync.api;
        return requestMES("回库嵌套", request, requestURL);
    }
}
